import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Put,
} from '@nestjs/common';
import { StyleService } from './style.service';
import { CreateStyleDto } from './dto/create-style.dto';
import { UpdateStyleDto } from './dto/update-style.dto';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { QueryPageDto } from '../../common/dto/query.page.dto';
import { QuerySizeDto } from '../size/dto/query-size.dto';
import { Prisma } from '@prisma/client';
import { QueryStyleDto } from './dto/query-style.dto';
import { CreateStyleProcessDto } from './dto/create-style-process.dto';
import { Roles } from '../../common/decorator/roles.decorator';
import { Role } from '../../common/types/role';
import { RolesGuard } from '../../common/guards/roles.guard';
import { strict } from 'node:assert';

@Controller('style')
@ApiTags('款式管理')
@Roles(Role.FINANCE)
@UseGuards(RolesGuard)
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class StyleController {
  constructor(private readonly styleService: StyleService) {}

  @Post()
  create(@Body() createStyleDto: CreateStyleDto) {
    return this.styleService.create(createStyleDto);
  }

  @Post(':id/process')
  createProcess(@Param('id') id: string, @Body() form: CreateStyleProcessDto) {
    return this.styleService.createProcess(id, form);
  }

  @Post('page')
  findByPage(@Body() query: QueryPageDto<QueryStyleDto>) {
    const where: Prisma.styleWhereInput = {
      modelId: query.model.modelId || undefined,
      groupId: query.model.groupId || undefined,
      isDelete: false,
    };

    return this.styleService.queryPage(query, where, {
      include: {
        model: {
          select: {
            id: true,
            name: true,
            code: true,
          },
        },
        group: {
          select: {
            id: true,
            name: true,
          },
        },
        sizes: {
          select: {
            sizeId: true,
            amount: true,
            size: true,
          },
        },
      },
    });
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.styleService.findOne(id);
  }

  @Put(':id')
  update(@Param('id') id: string, @Body() updateStyleDto: UpdateStyleDto) {
    return this.styleService.update(id, updateStyleDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.styleService.remove(id);
  }
}
